﻿using System;

namespace KPI
{
	public partial class KPIDetail : BasePage
	{
		private DataAccess.Models.KPI _TheKPI;
		private DataAccess.Repository.IKPIRepository _KPIRepository;

		public int TheKPIID
		{
			get
			{
				return CommonHelper.QueryStringInt("kpiid");
			}
		}

		public DataAccess.Models.KPI TheKPI
		{
			get
			{
				if(_TheKPI == null && TheKPIID > 0)
				{
					if(_KPIRepository == null)
					{
						_KPIRepository = new DataAccess.Repository.KPIRepository();
					}

					_TheKPI = _KPIRepository.GetTheKPI(TheKPIID);
				}

				return _TheKPI;
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindTheKPI();

				//var u = KPISiteContext.Current.CurrentUser;

				if(TheCurrentLoginUser != null && TheCurrentLoginUser.RoleName.Equals("administrators") && TheKPI != null)
				{
					if(TheKPI.CurrentState == DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Pending)
					{
						btnAgree.Visible = true;
						btnNotAgree.Visible = true;
					}
				}
			}
		}

		protected void btnReturn_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
		}

		protected void btnNotAgree_Click(object sender, EventArgs e)
		{
			if(TheKPI != null)
			{
				TheKPI.PreState = TheKPI.CurrentState;
				TheKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Not_Approved;

				if(_KPIRepository == null)
				{
					_KPIRepository = new DataAccess.Repository.KPIRepository();
				}

				if(_KPIRepository.UpdateKPI(TheKPI, true))
				{
					AddLatestInfo("KPI管理平台消息：您有一条KPI指标修改的已审核,但没有通过。", TheKPI);

					Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
				}
				else
				{
					ShowMessage("出错了,请稍后再试或联系相关技术人员.", true);
				}
			}
		}

		protected void btnAgree_Click(object sender, EventArgs e)
		{
			if(TheKPI != null)
			{
				TheKPI.PreState = TheKPI.CurrentState;
				TheKPI.CurrentState = DataAccess.Models.KPIStates.KPI_Of_Modification_Modified_Review_Approved;

				if(_KPIRepository == null)
				{
					_KPIRepository = new DataAccess.Repository.KPIRepository();
				}

				if(_KPIRepository.UpdateKPI(TheKPI, true))
				{
					AddLatestInfo("KPI管理平台消息：您有一条KPI指标修改的已审核通过。", TheKPI);
					
					Response.Redirect("~/KPIList.aspx?groupid=" + TheKPI.GroupID.ToString());
				}
				else
				{
					ShowMessage("出错了,请稍后再试或联系相关技术人员.", true);
				}
			}
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_KPIRepository != null)
			{
				((IDisposable)_KPIRepository).Dispose();
			}
		}

		private void BindTheKPI()
		{
			if(TheKPI != null)
			{
				lorgKPIActionPlan.Text = TheKPI.ActionPlans ?? string.Empty;
				lorgKPIContent.Text = TheKPI.KPIContent ?? string.Empty;
				lorgKPIFinishTime.Text = TheKPI.FinishTime.HasValue ? TheKPI.FinishTime.Value.ToString("yyyy-MM-dd") : string.Empty;
				lorgKPIResponsablePerson.Text = TheKPI.ResponsablePerson ?? string.Empty;
				lorgKPIResponseDept.Text = TheKPI.ResponseDepartment ?? string.Empty;
				lorgKPIResponsibleLeadership.Text = TheKPI.ResponsibleLeadership ?? string.Empty;
				lorgKPIRLTel.Text = TheKPI.RLTel ?? string.Empty;
				lorgKPIRPTel.Text = TheKPI.RPTel ?? string.Empty;
				lorgKPIStartTime.Text = TheKPI.StartTime.HasValue ? TheKPI.StartTime.Value.ToString("yyyy-MM-dd") : string.Empty;

				if(TheKPI.TheKPIForUpdate != null)
				{
					lUpdatedKPIActionPlan.Text = TheKPI.TheKPIForUpdate.UpdatedActionPlan??string.Empty;
					lUpdatedKPIContent.Text = TheKPI.TheKPIForUpdate.UpdatedKPIContent ?? string.Empty;
					lUpdatedKPIFinishTime.Text = TheKPI.TheKPIForUpdate.UpdatedFinishTime.HasValue ? TheKPI.TheKPIForUpdate.UpdatedFinishTime.Value.ToString("yyyy-MM-dd") : string.Empty;
					lUpdatedKPIResponsablePerson.Text = TheKPI.TheKPIForUpdate.UpdatedResponsablePerson ?? string.Empty;
					lUpdatedKPIResponseDept.Text = TheKPI.TheKPIForUpdate.UpdatedResponseDepartment ?? string.Empty;
					lUpdatedKPIResponsibleLeadership.Text = TheKPI.TheKPIForUpdate.UpdatedResponsibleLeadership ?? string.Empty;
					lUpdatedKPIRLTel.Text = TheKPI.TheKPIForUpdate.UpdatedRLTel ?? string.Empty;
					lUpdatedKPIRPTel.Text = TheKPI.TheKPIForUpdate.UpdatedRPTel ?? string.Empty;
					lUpdatedKPIStartTime.Text = TheKPI.TheKPIForUpdate.UpdatedStartTime.HasValue ? TheKPI.TheKPIForUpdate.UpdatedStartTime.Value.ToString("yyyy-MM-dd") : string.Empty;
				}
			}
		}
	}
}